package fr.ece.ing4.si.monteCarlo;
import fr.ece.ing4.si.monteCarlo.thread.*;

import java.awt.GridLayout;

import java.awt.*;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.concurrent.ExecutionException;


import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;


import javax.swing.*;



public class Interface extends JFrame implements ActionListener {
	private static final long serialVersionUID = 1L;

	private int load;
    private JButton run;
    private JLabel result, time;
    private long setTime, val1, val2;
    private double optionResult, optionResult2;
    
    //combobox declarations
	private JComboBox<String> comboBox1;
	private JComboBox<String> comboBox2;
	private JComboBox<Double> comboBox3;
	private JComboBox<Double> comboBox4;
	private JComboBox<Double> comboBox5;
	private JComboBox<Double> comboBox6;
	private JComboBox<Double> comboBox7;
	private JComboBox<Double> comboBox8;
	private JComboBox<Double> comboBox9;
	private JComboBox<Double> comboBox10;
	
	//Jlabel declarations
	private JLabel name1;
	private JLabel name2;
	private JLabel name3;
	private JLabel name4;
	private JLabel name5;
	private JLabel name6;
	private JLabel name7;
	private JLabel name8;
	private JLabel name9;
	private JLabel name10;
	private JLabel name11;
    //private JProgressBar progressCalculation;

	

	 public BufferedReader br;


    protected void frameInit() {
        super.frameInit();
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        getContentPane().setLayout(new GridLayout(6, 3, 6, 3));

        run = new JButton();
        run.setText("Run");
        run.setForeground(Color.BLUE);


        result = new JLabel();
        time = new JLabel();
        result.setText("Result");
        result.setForeground(Color.BLUE);
        time.setText("Time");
        time.setForeground(Color.BLUE);

        //progressCalculation = new JProgressBar(0, 100);
       // progressCalculation.setValue(0);
        //progressCalculation.setStringPainted(true);
        
        
        name1=new JLabel("             Select Thread Type:");
        name2=new JLabel("                    Call or Put:");
        name3=new JLabel("             Stock price today:");
        name4=new JLabel("                    Strike price:");
        name5=new JLabel("             Discount rate:");
        name6=new JLabel("                    Time to maturity:");
        name7=new JLabel("             Cost of carry rate:");
        name8=new JLabel("                    Volatility:");
        name9=new JLabel("             nSteps:");
        name10=new JLabel("                   nSimulations:");
        name11=new JLabel("  ");
        

        comboBox1 = new JComboBox<String>();
        comboBox1.addItem("Single Thread");
        comboBox1.addItem("Multi Thread");
        comboBox1.setEditable(false);       
        
        comboBox2 = new JComboBox<String>();
        comboBox2.addItem("call");
        comboBox2.addItem("put");
        comboBox2.setEditable(false);
        
        comboBox3 = new JComboBox<Double>();
        comboBox4 = new JComboBox<Double>();
        comboBox5 = new JComboBox<Double>();
        comboBox6 = new JComboBox<Double>();
        comboBox7 = new JComboBox<Double>();
        comboBox8 = new JComboBox<Double>();
        comboBox9 = new JComboBox<Double>();
        comboBox10 = new JComboBox<Double>();
        ArrayList<Object> receiveArg = new ArrayList<Object>();
       
        for(load=1;load<=10;load++){
        	 
             try {
                 receiveArg = passingArg(load);
                 //load = 0;

             } catch (IOException e) {
                 // TODO Auto-generated catch block
                 e.printStackTrace();
             }
           
             
        double tmp=Double.parseDouble((String) receiveArg.get(1)); 
        comboBox3.addItem(tmp);
        
        tmp=Double.parseDouble((String) receiveArg.get(2));
        comboBox4.addItem(tmp);
        
        tmp=Double.parseDouble((String) receiveArg.get(3));
        comboBox5.addItem(tmp);
        
        tmp=Double.parseDouble((String) receiveArg.get(4));        
        comboBox6.addItem(tmp);
        
        tmp=Double.parseDouble((String) receiveArg.get(5));        
        comboBox7.addItem(tmp);
        
        tmp=Double.parseDouble((String) receiveArg.get(6));
        comboBox8.addItem(tmp);
        
        tmp=Double.parseDouble((String) receiveArg.get(7));
        comboBox9.addItem(tmp);
        
        tmp=Double.parseDouble((String) receiveArg.get(8));
        comboBox10.addItem(tmp);
        
         }      
        comboBox3.setEditable(false);
        comboBox4.setEditable(false);
        comboBox5.setEditable(false);
        comboBox6.setEditable(false);
        comboBox7.setEditable(false);
        comboBox8.setEditable(false);
        comboBox9.setEditable(false);
        comboBox10.setEditable(false);
        
        
        
        getContentPane().add(result);
        getContentPane().add(time);
        //getContentPane().add(progressCalculation);
        getContentPane().add(name11);
        getContentPane().add(run);

        
        getContentPane().add(name1);
        getContentPane().add(comboBox1);
        getContentPane().add(name2);
        getContentPane().add(comboBox2);
        getContentPane().add(name3);
        getContentPane().add(comboBox3);
        getContentPane().add(name4);
        getContentPane().add(comboBox4);
        getContentPane().add(name5);
        getContentPane().add(comboBox5);
        getContentPane().add(name6);
        getContentPane().add(comboBox6);
        getContentPane().add(name7);
        getContentPane().add(comboBox7);
        getContentPane().add(name8);
        getContentPane().add(comboBox8);
        getContentPane().add(name9);
        getContentPane().add(comboBox9);
        getContentPane().add(name10);
        getContentPane().add(comboBox10);
    

        run.addActionListener(this);


    }

    @Override
    public void actionPerformed(ActionEvent arg0) {


        name11.setText("Calculation Processed !");

    		
    	String thread = (String) comboBox1.getSelectedItem();
    	String callPutFlag = (String) comboBox2.getSelectedItem();
        double s = (Double) comboBox3.getSelectedItem();
        double x = (Double) comboBox4.getSelectedItem();
        double t = (Double) comboBox5.getSelectedItem();
        double r = (Double) comboBox6.getSelectedItem();
        double b = (Double) comboBox7.getSelectedItem();
        double v = (Double) comboBox8.getSelectedItem();
        double nSteps = (Double) comboBox9.getSelectedItem();
        double nSimulations = (Double) comboBox10.getSelectedItem();
        
        System.out.println("Output" +thread);
        System.out.println("Output" +callPutFlag);
        System.out.println("Output" +s);
        System.out.println("Output" +x);
        System.out.println("Output" +t);
        System.out.println("Output" +r);
        System.out.println("Output" +b);
        System.out.println("Output" +v);
        System.out.println("Output" +nSteps);
        System.out.println("Output" +nSimulations);
        
        
        if(thread=="Single Thread"){
            MonteCarloThread object=new MonteCarloThread();

            val1 = System.currentTimeMillis();
            optionResult = object.monteCarloStandardOption1(callPutFlag, s, x, t, r, b, v, nSteps, nSimulations);

            val2 = System.currentTimeMillis();
            setTime = val2 - val1;
            result.setText(Double.valueOf(optionResult).toString());
            time.setText(Long.valueOf(setTime).toString());

    
    }//end if
    else if(thread=="Multi Thread"){
    	MonteCarloMultiThread object=new MonteCarloMultiThread();
    	val1 = System.currentTimeMillis();    	
            try {
				optionResult2 = object.monteCarloStandardOption2(callPutFlag, s, x, t, r, b, v, nSteps, nSimulations);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ExecutionException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}


        val2 = System.currentTimeMillis();
        setTime = val2 - val1;

        result.setText(Double.valueOf(optionResult2).toString());

        time.setText( Long.valueOf(setTime).toString() );
    }
    }

    public ArrayList<Object> passingArg(int i) throws IOException {
        br = new BufferedReader(new FileReader("ArgumentsSet.txt"));


        final ArrayList<Object> listArg = new ArrayList<Object>();
        String readline = "";
        String[] val;

        while (i > 0) {
            readline = br.readLine();
            i--;
        }

        val = readline.split(",");
        for (int j = 0; j < val.length; j++) {
            listArg.add(val[j]);
        }
        return listArg;
    }

}